package com.bawei.module_home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_common.cache.RecyclerCachePool
import com.bawei.module_home.cache.HomeItemCache
import com.bawei.module_home.databinding.ItemVideoLayoutBinding
import com.bawei.module_home.entriy.SimpleVideoEntity
import com.example.image.glide.GlideImgLoader
import kotlin.math.max

class HomeListAdapter constructor(private val list:MutableList<SimpleVideoEntity> ):RecyclerView.Adapter<HomeListAdapter.ItemViewHolder>() {

    //滚动停止
    private var ScrollState=RecyclerView.SCROLL_STATE_IDLE
    //倒数第几个触发预加载
    var preloadEndCount=0
    //是否在预加载中
    private var isPreloading=false
    var preloadMoreData:(()->Unit)?=null//回调方法

    var itemViewOut:HomeItemCache?=null
    private var itemClickListener: OnItemClickListener ?= null

    fun setOnItemClickListener(l: OnItemClickListener){
        itemClickListener = l
    }


    inner class ItemViewHolder(val dataBinding: ItemVideoLayoutBinding): RecyclerView.ViewHolder(dataBinding.root)

    //点击事件接口
    interface OnItemClickListener{
        fun onItemChick(v: View, position: Int)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        var itemViewHolder = ItemViewHolder(ItemVideoLayoutBinding.inflate(LayoutInflater.from(parent.context),parent,false))

        itemViewOut?.addViewHolder(viewType,itemViewHolder)
        return itemViewHolder
    }

    override fun getItemCount(): Int {
      return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return RecyclerCachePool.HOME_LIST_TIEM_VIEW_TYPE
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }


    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val entity = list[position]
        //视频标题和描述
        holder.dataBinding.tvTitle.text = entity.title
        holder.dataBinding.tvDesc.text = entity.description
        holder.dataBinding.ivVideoThumbnail.apply {
            setUp(entity.videopath,true,"")
            //自动播放
            isStartAfterPrepared = true // 关键配置：准备完成后自动播放
            startPlayLogic()
        }

        GlideImgLoader.loadCircleImage(holder.dataBinding.root.context, entity.avatar_url, holder.dataBinding.ivAvatar)
        //播放按钮点击事件
        holder.dataBinding.tvTitle.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }

        //评论按钮点击事件
        holder.dataBinding.ivCommend.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }

        //更多按钮点击事件
        holder.dataBinding.ivMore.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }



        //检查是否需要加载下一页数据
        checkPreLoadMoreData(position)
    }
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        if (itemViewOut !=null){
            recyclerView.setViewCacheExtension(itemViewOut)
        }
        //添加滚动状态
        recyclerView.addOnScrollListener(object :RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                ScrollState=newState
                super.onScrollStateChanged(recyclerView, newState)
            }
        })
    }
    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        if (itemViewOut !=null){
            recyclerView.setViewCacheExtension(itemViewOut)
        }
    }
    /**
     * 数据预加载
     */
    private fun checkPreLoadMoreData(position: Int) {
        if(ScrollState!=RecyclerView.SCROLL_STATE_IDLE//滚动中
            && position== max(list.size-1-preloadEndCount,0)//当前页面最后第几个
            && !isPreloading
        ){
            //正在触发加载标识
            isPreloading=true
            //执行回调方法
            preloadMoreData?.invoke()
        }else{
            isPreloading=false
        }
    }
}